<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数:{{ total }}条</span>
        </template>
        <template #right>
          <el-button type="warning" size="small" @click="$router.push('/excel')">excel导入</el-button>
          <el-button type="danger" size="small" @click="hExport">excel导出</el-button>
          <el-button type="primary" size="small" @click="dialogVisible=true">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px;">
        <el-table border :data="list">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column
            label="聘用形式"
            prop="formOfEmployment"
          >
            <template #default="{row}">
              {{ hireType[row.formOfEmployment] }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="correctionTime" />
          <el-table-column label="账户状态" prop="enableState" />
          <el-table-column label="操作" width="280">
            <template #default="scope">
              <el-button type="text" size="small" @click="$router.push('/employees/detail?id='+scope.row.id)">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small" @click="hDel(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <div class="block">
            <el-pagination
              :current-page="page"
              :page-sizes="[2, 5, 10, 20]"
              :page-size="5"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </el-row>
      </el-card>
    </div>
    <el-dialog
      title="添加"
      :visible.sync="dialogVisible"
      width="50%"
      @close="hClose"
    >
      <empdialog ref="empdialog" @closeDialog="closeDialog" />
    </el-dialog>
  </div>
</template>

<script>
import empdialog from './empdialog.vue'
import employees from '@/constant/employees'
const obj = {}
employees.hireType.forEach((item) => { obj[item.id] = item.value })
import { delEmployee, getEmployeesList } from '@/api/employees'
export default {
  components: { empdialog },
  data() {
    return {
      dialogVisible: false,
      hireType: obj,
      list: [],
      page: 1,
      size: 5,
      total: 0
    }
  },
  created() {
    this.loadList()
  },
  methods: {
    hDel(id) {
      this.$confirm('确认删除吗？').then(() => {
        delEmployee(id)
        if (this.list.length === 1) this.page--
        this.loadList()
      })
    },
    handleSizeChange(val) {
      this.page = 1
      this.size = val
      this.loadList()
    },
    handleCurrentChange(val) {
      this.page = val
      this.loadList()
    },
    async loadList() {
      try {
        const res = await getEmployeesList({ page: this.page, size: this.size })
        this.list = res.data.rows
        this.total = res.data.total
        // 保存数据
      } catch (err) {
        console.log('getEmployeesList', err)
      }
    },
    hClose() {
      this.$refs.empdialog.resetForm()
    },
    closeDialog() {
      if (this.list.length === this.size) this.page++
      this.loadList()
      this.dialogVisible = false
    },
    hExport() {
      import('@/vendor/Export2Excel').then(excel => {
        const { tHeader, data } = this.formatData(this.list)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: this.filename,
          autoWidth: this.autoWidth,
          bookType: this.bookType
        })
        this.downloadLoading = false
      })
    },
    formatData(list) {
      const mapInfo = {
        'id': '编号',
        'password': '密码',
        'mobile': '手机号',
        'username': '姓名',
        'timeOfEntry': '入职日期',
        'formOfEmployment': '聘用形式',
        'correctionTime': '转正日期',
        'workNumber': '工号',
        'departmentName': '部门',
        'staffPhoto': '头像地址',
        'enableState': '账户状态'
      }
      const one = list[0]
      if (!one) return
      const tHeader = Object.keys(one).map(item => {
        return mapInfo[item]
      })
      const data = list.map(item => {
        return Object.values(item)
      })
      return { tHeader, data }
    }
  }
}
</script>
